package com.huaruan.youdi.views.activitys;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.CountDownTimer;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.huaruan.youdi.R;
import com.huaruan.youdi.http.okhttp.Constant;
import com.huaruan.youdi.http.okhttp.HttpCallback;
import com.huaruan.youdi.http.okhttp.HttpUtils;
import com.huaruan.youdi.models.RegisterModel;
import com.huaruan.youdi.utils.AppUtils;
import com.huaruan.youdi.utils.StringUtils;

import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;

public class RegisterActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_rag_code)
    EditText et_rag_code;
    @BindView(R.id.tv_get_reg_code)
    TextView tv_get_reg_code;
    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.iv_password_visible)
    ImageView iv_password_visible;
    @BindView(R.id.tv_next)
    TextView tv_next;

    private String userPhone;
    private String userPassword;
    private String ragCode;

    private TimeCount timeCount;
    private boolean isPasswordVisible;
    RegisterModel model;


    @Override
    public int IGetContentViewResId() {
        return R.layout.activity_register;
    }

    @Override
    public void IFindViews() {
        timeCount = new TimeCount(60000, 1000);
        iv_password_visible.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPasswordVisible();
            }
        });
        tv_get_reg_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userPhone = et_phone.getText().toString();
                if (StringUtils.isEmpty(userPhone)) {
                    AppUtils.toastShort(IGetContext(), "请输入您的手机号");
                    return;
                }
                if (userPhone.length() != 11) {
                    AppUtils.toastShort(IGetContext(), "请输入正确的手机号");
                    return;
                }
                sendMsg();
            }
        });

        tv_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                forgetPwd();
            }
        });

    }

    @Override
    public void IInitData() {
        setPasswordVisible();
    }

    private void setPasswordVisible() {
        isPasswordVisible = !isPasswordVisible;
        if (isPasswordVisible) {
            iv_password_visible.setImageResource(R.mipmap.icon_password_visible);
            et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            iv_password_visible.setImageResource(R.mipmap.icon_password_invisible);
            et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }

    private void sendMsg() {
        userPhone = et_phone.getText().toString();
        if (StringUtils.isEmpty(userPhone)) {
            AppUtils.toastShort(IGetContext(), "请输入您的手机号");
            return;
        }
        SortedMap<String, String> mSortedMap = new TreeMap<>();
        mSortedMap.put("phone", userPhone);
        new HttpUtils.Builder(IGetContext())
                .postMethod()
                .url(Constant.SEND_MESSAGE)
                .params(mSortedMap)
                .modelClass(RegisterModel.class)
                .callback(new HttpCallback() {
                    @Override
                    public void success(Object object) {
                        AppUtils.toastShort(IGetContext(), "获取验证码成功");
                        timeCount.start();
                        model = (RegisterModel) object;
                    }
                }).start();
    }

    private void forgetPwd() {
        userPhone = et_phone.getText().toString();
        userPassword = et_password.getText().toString();
        ragCode = et_rag_code.getText().toString();
        if (StringUtils.isEmpty(userPhone)) {
            AppUtils.toastShort(IGetContext(), "请输入您的手机号");
            return;
        }
        if (userPhone.length() != 11) {
            AppUtils.toastShort(IGetContext(), "请输入正确的手机号");
            return;
        }
        if (StringUtils.isEmpty(ragCode)) {
            AppUtils.toastShort(IGetContext(), "请输入验证码");
            return;
        }
        if (StringUtils.isEmpty(userPassword)) {
            AppUtils.toastShort(IGetContext(), "请输入您的密码");
            return;
        }
        if (model == null) {
            return;
        }

        SortedMap<String, String> mSortedMap = new TreeMap<>();
        mSortedMap.put("phone", userPhone);
        mSortedMap.put("password", userPassword);
        mSortedMap.put("msgNum", ragCode);
        mSortedMap.put("tamp", model.tamp);
        mSortedMap.put("hash", model.hash);
        new HttpUtils.Builder(IGetContext())
                .postMethod()
                .url(Constant.FORGET_PASSWORD)
                .params(mSortedMap)
                .callback(new HttpCallback() {
                    @Override
                    public void success(Object object) {
                        AppUtils.toastShort(IGetContext(),"修改密码成功");
                        AppUtils.jump(IGetContext(), LoginActivity.class);
                        finish();
                    }
                }).start();
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onFinish() {
            tv_get_reg_code.setText("重新验证");
            tv_get_reg_code.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tv_get_reg_code.setClickable(false);
            tv_get_reg_code.setText("等待" + millisUntilFinished / 1000 + "秒");
        }
    }

}
